/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-10-25 16:32:10
 * @LastEditors: wuhao930406 1148547900@qq.com
 * @LastEditTime: 2023-10-27 14:53:49
 * @FilePath: /eggauthmoblie/app/controller/wechat.js
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
'use strict';

const { Controller } = require('egg');

// 排序的函数

class WechatController extends Controller {
  async validate() {
    const { ctx, service } = this;
    ctx.body = await service.wx.checkSign();
  }

  // 返回access——token给前端
  async authtoken() {
    const { ctx, service, app } = this;
    const { code } = ctx.request.body;
    console.log(code, 'code');
    // await app.redis.flushall();

    if (!code) {
      ctx.body = {
        code: -1,
        msg: '获取失败配置错误',
      };
      return;
    }

    const access_token = await service.wx.fetchAccessToken(code);
    if (access_token) {
      ctx.body = {
        code: 1,
        msg: 'success',
        data: access_token,
      };
    } else {
      ctx.body = {
        code: -1,
        msg: '获取失败配置错误',
      };
    }
  }

  async getticket() {
    // appsecret
    const { ctx, service } = this;
    const { url } = ctx.request.body;
    const sign = await service.wx.getJsSdkSign(url);
    if (sign) {
      ctx.body = {
        code: 1,
        msg: 'success',
        data: sign,
      };
    } else {
      ctx.body = {
        code: -1,
        msg: '获取失败配置错误',
      };

    }
  }
}

module.exports = WechatController;
